package com.yy.train.ticket.train.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.train.ticket.goout.trs.BoxSeat;
import com.yy.train.ticket.goout.TrainBox;
import com.yy.train.ticket.into.page.BoxPage;
import com.yy.train.ticket.train.dao.TtTrainBoxDao;
import com.yy.train.ticket.train.dao.TtTrainSeatDao;
import com.yy.train.ticket.train.entity.TtTrainBoxEntity;
import com.yy.train.ticket.train.entity.TtTrainSeatEntity;
import com.yy.train.ticket.train.service.TtTrainBoxService;
import com.yy.train.ticket.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service("ttTrainBoxService")
public class TtTrainBoxServiceImpl extends ServiceImpl<TtTrainBoxDao, TtTrainBoxEntity> implements TtTrainBoxService {
@Resource
    TtTrainBoxDao ttTrainBoxDao;
@Resource
    TtTrainSeatDao ttTrainSeatDao;

    @Override
    public R saveBox(TtTrainBoxEntity ttTrainBox) {
        Integer seat= ttTrainBox.getSeat();
        Long trainId=ttTrainBox.getTrainId();

        ttTrainBoxDao.updateById(ttTrainBox);
        Long boxId = ttTrainBox.getBoxId();
        List<TtTrainSeatEntity> list=new ArrayList<>();
        TtTrainSeatEntity trainSeat;
        for (int i = 1; i <seat+1 ; i++) {
            trainSeat=new TtTrainSeatEntity();
            trainSeat.setSeatNo(String.valueOf(i));
            trainSeat.setTrainId(trainId);
            trainSeat.setBoxId(boxId);
            list.add(trainSeat);
            ttTrainSeatDao.insert(trainSeat);
        }
        return R.ok();
    }

    @Override
    public R updateTrainBoxById(TtTrainBoxEntity ttTrainBox) {
        Integer seat= ttTrainBox.getSeat();
        Long trainId=ttTrainBox.getTrainId();
        Long boxId=ttTrainBox.getBoxId();
        ttTrainBoxDao.updateById(ttTrainBox);
     List<TtTrainSeatEntity> list=   ttTrainSeatDao.selectList(Wrappers.<TtTrainSeatEntity>lambdaQuery().eq(TtTrainSeatEntity::getBoxId,boxId));
       if(!list.isEmpty()){

           List<Long> boxIds=list.stream().map(TtTrainSeatEntity::getBoxId).collect(Collectors.toList());
           ttTrainSeatDao.deleteBatchIds(boxIds);
       }
        TtTrainSeatEntity trainSeat=null;
        for (int i = 1; i <seat+1 ; i++) {
            trainSeat=new TtTrainSeatEntity();
            trainSeat.setSeatNo(String.valueOf(i));
            trainSeat.setTrainId(trainId);
            trainSeat.setBoxId(boxId);
            ttTrainSeatDao.insert(trainSeat);
        }

        return R.ok();
    }

    @Override
    public R boxPage(BoxPage boxPage) {


        Page<BoxPage> page = new Page<>(boxPage.getPageNum(), boxPage.getPageSize());

        IPage<TrainBox> list = ttTrainBoxDao.boxPage(page, boxPage);
        return R.ok(list);
    }

    @Override
    public R getTrainBoxSeat(Long trainId) {
        List<BoxSeat> list = ttTrainBoxDao.getTrainBoxSeat(trainId);
        return R.ok(list);
    }
}